﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FisheryPlatform.Common
{
    /// <summary>
    /// 呈现用户控件内容
    /// </summary>
    /// <typeparam name="T">UserControl</typeparam>
    public class UserControlPreviewManager<T> where T : System.Web.UI.UserControl
    {
        private System.Web.UI.Page _MainPage;

        /// <summary>
        /// 加载指定的虚路径用户控件
        /// </summary>
        /// <param name="UserControlPath"></param>
        /// <returns></returns>
        public T LoadUserControl(string userControlVirtualPath)
        {
            _MainPage = new System.Web.UI.Page();

            return (T)_MainPage.LoadControl(userControlVirtualPath);
        }

        /// <summary>
        /// 加载指定的虚路径用户控件, 根据具体的数组调用匹配构造函数
        /// </summary>
        /// <param name="userControlVirtualPath"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T LoadUserControl(Type type, object[] parameters)
        {
            _MainPage = new System.Web.UI.Page();

            return (T)_MainPage.LoadControl(type, parameters);
        }

        /// <summary>
        /// 呈现控件Html
        /// </summary>
        /// <param name="userControl"></param>
        /// <returns></returns>
        public string RenderHtml(T userControl)
        {
            StringWriter outputWriter = new StringWriter();

            _MainPage.Controls.Add(userControl);
            System.Web.HttpContext.Current.Server.Execute(_MainPage, outputWriter, false);

            return outputWriter.ToString();
        }
    }
}
